package Group9.TodoManager.Actions;

import Group9.TodoManager.Data.Interface.ITableModel;
import Group9.TodoManager.DataStore.Repository;
import Group9.TodoManager.Settings;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * This action is meant for removing the selected task from the repository
 * @author volkan cambazoglu
 * @author Rupesh
 */
public class RemoveAction<T> extends TodoManagerAction {
	
    private ITableModel<T> tableModel;
    private Repository<T> repository;

    /**
     * Constructor. Creates an instance of the RemoveAction class.
     * @param name name of the action
     * @param icon icon of the action
     * @param description description of the action
     * @param mnemonic mnemonic of the action
     * @param table table model
     * @param repo repository
     */
    public RemoveAction(String name,ImageIcon icon, String description,  
    				int mnemonic, ITableModel<T> table, Repository<T> repo){
        super(name, icon);
        putValue(Action.SHORT_DESCRIPTION, description);
        putValue(Action.MNEMONIC_KEY, mnemonic);
        tableModel = table;
        repository = repo;
    }

    /**
     * When a task will be removed, this method will be executed to handle
     * remove operation.
     * @param e event
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        int selectedIndex = tableModel.getSelectedRowIndex();
        T selectedTask = tableModel.getRowObject(selectedIndex);

        if (JOptionPane.showConfirmDialog(Settings.getMainGUI(),
        		Settings.getCurrentResourceBundle().getString("Message.DeleteConfirmMsg"), 
        		Settings.getCurrentResourceBundle().getString("Message.DeleteConfirmTitle"),
        		JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
        	
            repository.removeItem(selectedTask);
            repository.Commit();
            Settings.displayMessage(
            	Settings.getCurrentResourceBundle().getString("Message.TaskRemoved"));
            tableModel.removeRowAt(selectedIndex);
            Settings.getMainGUI().removeFromList(selectedTask);
        	Settings.getMainGUI().resetTxtarea();
        	Settings.getMainGUI().resetProgressModel();
        }
    }
}